﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;

namespace DAL
{
    public class AutoGenCode
    {
        public static string GenNewCode(string tableName)
        {
            SqlCommand cmd = new SqlCommand(String.Format("select * from {0}", tableName), DataProvider.DataAccess.GetConnection());
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);

            string prefix = "", sufix = "";
            int lenghtCode;
            string maxCode = dt.Rows[dt.Rows.Count - 1][0].ToString();
            lenghtCode = maxCode.Length;
            foreach (char c in maxCode)
                if (c >= 58)
                    prefix += c;
                else
                    sufix += c;
            sufix = (Convert.ToInt32(sufix) + 1).ToString();
            while (sufix.Length + prefix.Length < lenghtCode)
                sufix = "0" + sufix;
            return prefix + sufix;
        }

        public static int GetRealIndexOfRow(string key,DataView dgv,int cellNumber)
        {
            //MessageBox.Show(dgv.Table.Rows.Count.ToString());
            for (int i = 0; i < dgv.Table.Rows.Count; i++)
            {
                //Console.WriteLine(dgv.Table.Rows[i][cellNumber].ToString());
                if (dgv.Table.Rows[i][cellNumber].ToString() == key)
                    return i;
            }
            return 0;
        }
    }
}
